package com.dmx.adi;


import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dmx.cep.entity.VodContentInfo;
import com.dmx.cep.entity.VodContentStatus;
import com.dmx.cep.service.ContentService;
import com.dmx.fw.util.DateTools;
import com.dmx.net.ConnectionException;
import com.dmx.net.HostInfoExtractor;
import com.dmx.net.ftp.impl.FtpClient;
import com.dmx.net.ftp.impl.RemoteFtpFileNotExistsException;

public class AdiParseUtil {
	private String recomm;
//	private VodContentService  vodRSServiceProxy;
	private static Log log=LogFactory.getLog(AdiParseUtil.class);
	
	public static void adiValidation(VodContentInfo contentInfo,String ftpUrl,ContentService contentService) throws Exception{
		
		try {
			byte[] adiXml = ADIUtil.download2(ftpUrl);

			if (adiXml == null || adiXml.length <= 0)
				throw new ADIException("ADI parse exception:ftp file can not be accessed,or the file is not exists.");

			MetadataParser parser = new VODMetadataParser(adiXml);

			ADIMetadata adidata = new ADIMetadata();
			parser.parse(adidata);

			Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
			map = getMetaData(map, adidata);
			Map<String, String> packageMap = map.get("package");
			if(packageMap==null||packageMap.size()==0){
				throw new ADIFormatException("ADI format error,missing package elements.");
			}

			Map<String, String> titleMap = map.get("title");
			if(titleMap==null||titleMap.size()==0){
				throw new ADIFormatException("ADI format error,missing title elements.");
			}
			Map<String, String> appdataTitleMap = map.get("APPDATAtitle");
			parseTitleMap2(titleMap, appdataTitleMap, contentInfo);
			
			Map<String, String> movieMap = map.get("movie");
			if(movieMap==null||movieMap.size()==0){
				throw new ADIFormatException("ADI format error,missing movie elements.");
			}
			String movieAssetId = getValueFromMap("Asset_ID", movieMap);
			contentInfo.setContid(movieAssetId);
			
			
			
			//Map<String, String> appdataMovieMap = map.get("APPDATAmovie");
			Map<String, String> posterMap = map.get("poster");
			/*if(posterMap==null||posterMap.size()==0){
				throw new ADIFormatException("ADI format error,missing poster elements.");
			}*/
			// Map<String, String> appdataPosterMap = map.get("APPDATAposter");


			String movieContent = getValueFromMap("Content", movieMap);
			if(movieContent==null||"".equals(movieContent.trim())){
				throw new VideoNotFoundException("ADI parse error,missing video file.");
			}
			
			String posterContent = getValueFromMap("Content", posterMap);
			
			VodContentStatus contentStatus = (VodContentStatus) contentService
					.getObject(VodContentStatus.class, movieAssetId);
			
			if (contentStatus!=null&&contentStatus.getIsValid()!=0&&contentStatus.getIsValid()!=1) {
				throw new AssetStatusUpdateException("Asset is being destroyed,can't not be updated.");
			}

			String contentUrl = ftpUrl.substring(0, ftpUrl.lastIndexOf('/'));
			String movieContentUrl = contentUrl + "/" + movieContent;
			String posterUrl =null; 
			if(posterContent!=null&&!"".equals(posterContent.trim()))
				posterUrl=contentUrl + "/" + posterContent;

			/**
			 * 检查视频文件是否存在
			 */
			if(!testFtpFileExists(movieContentUrl)){
				log.error("movie content not exists:"+movieContentUrl+".");
				throw new VideoNotFoundException("Video file was not found in FTP directory.");
			}
			
			/**
			 * 检查海报文件是否存在
			 */
			if(posterUrl!=null&&!testFtpFileExists(posterUrl)){
				log.error("poster not exists:"+posterUrl+".");
				throw new PosterNotFoundException("Poster file was not found in FTP directory.");
			}
			
			log.info("validate asset[assetId="+movieAssetId+"] success.");
		} catch (Exception e) {
			log.error("validate ADI error:",e);
			throw e;
		}
	}	
	
	public static void adiValidation2(VodContentInfo contentInfo,String ftpUrl) throws Exception{
		
		byte[] adiXml = ADIUtil.download2(ftpUrl);

		if (adiXml == null || adiXml.length <= 0)
			throw new ADIException("ADI parse exception:ftp file can not be accessed,or the file is not exists.");

		MetadataParser parser = new VODMetadataParser(adiXml);

		ADIMetadata adidata = new ADIMetadata();
		parser.parse(adidata);

		Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
		map = getMetaData(map, adidata);

		Map<String, String> titleMap = map.get("title");
		Map<String, String> appdataTitleMap = map.get("APPDATAtitle");
		parseTitleMap2(titleMap, appdataTitleMap, contentInfo);

		Map<String, String> movieMap = map.get("movie");
		String movieAssetId = getValueFromMap("Asset_ID", movieMap);
		contentInfo.setContid(movieAssetId);

	}	
	
//	public static void adiImport(String ftpUrl,ContentService contentService ,VodContentService vodRSServiceProxy,String recomm) throws Exception{
//		
//		try {
//			log.info("ftp url="+ftpUrl);
//			byte[] adiXml = ADIUtil.download2(ftpUrl);
//;
//
//			if (adiXml == null || adiXml.length <= 0)
//				throw new ADIException("ADI parse exception:ftp file can not be accessed,or the file is not exists.");
//
//			MetadataParser parser = new VODMetadataParser(adiXml);
//            
//			ADIMetadata adidata = new ADIMetadata();
//			parser.parse(adidata);
//
//			Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
//			map = getMetaData(map, adidata);
//			Map<String, String> packageMap = map.get("package");
//			if(packageMap==null||packageMap.size()==0){
//				throw new ADIFormatException("ADI format error,missing package elements.");
//			}
//			Map<String, String> appdataPackageMap = map.get("APPDATApackage");
//			
//
//			String product = getValueFromMap("Product", packageMap);
//			String defaultLanguage = getValueFromMap(product
//					+ "::Default_Language", appdataPackageMap);
//			String provider = getValueFromMap("Provider", packageMap);
//			String providerId = getValueFromMap("Provider_ID", packageMap);
//
//			Map<String, String> titleMap = map.get("title");
//			if(titleMap==null||titleMap.size()==0){
//				throw new ADIFormatException("ADI format error,missing title elements.");
//			}
//			Map<String, String> appdataTitleMap = map.get("APPDATAtitle");
//			Map<String, String> movieMap = map.get("movie");
//			if(movieMap==null||movieMap.size()==0){
//				throw new ADIFormatException("ADI format error,missing movie elements.");
//			}
//			Map<String, String> appdataMovieMap = map.get("APPDATAmovie");
//			Map<String, String> posterMap = map.get("poster");
//			/*if(posterMap==null||posterMap.size()==0){
//				throw new ADIFormatException("ADI format error,missing poster elements.");
//			}*/
//			// Map<String, String> appdataPosterMap = map.get("APPDATAposter");
//
//			String movieAssetId = getValueFromMap("Asset_ID", movieMap);
//			String movieContent = getValueFromMap("Content", movieMap);
//			if(movieContent==null||"".equals(movieContent.trim())){
//				throw new VideoNotFoundException("ADI parse error,missing video file.");
//			}
//			
//			String posterContent = getValueFromMap("Content", posterMap);
//
//			VodContentInfo contentInfo = (VodContentInfo) contentService
//					.getObject(VodContentInfo.class, movieAssetId);
//			
//			VodContentStatus contentStatus = (VodContentStatus) contentService
//					.getObject(VodContentStatus.class, movieAssetId);
//			
//			if (contentInfo == null) {
//				contentInfo = new VodContentInfo();
//
//				contentInfo.setContid(movieAssetId);
//
//			} else {
//				if(-1 == contentInfo.getStatus()){
//					if (contentStatus!=null&&(contentStatus.getIsValid()==0||contentStatus.getIsValid()==1)) {
//						
//						/**
//						 * 资产状态恢佛
//						 */
//						contentInfo.setStatus(0);
//						contentStatus.setIsValid(1);
//					}else{
//						throw new AssetStatusUpdateException("Asset is being destroyed,can't not be updated.");
//					}
//				}
//			}
//
//			contentInfo.setLanguage(defaultLanguage);
//			contentInfo.setProviderName(provider);
//			contentInfo.setProviderId(providerId);
//			contentInfo.setCreationDate(new Date());
//
//			parseTitleMap(titleMap, appdataTitleMap, contentInfo);
//
//			parseMovieMap(movieMap, appdataMovieMap, contentInfo);
//
//			String contentUrl = ftpUrl.substring(0, ftpUrl.lastIndexOf('/'));
//			String movieContentUrl = contentUrl + "/" + movieContent;
//			String posterUrl =null; 
//			if(posterContent!=null&&!"".equals(posterContent.trim()))
//				posterUrl=contentUrl + "/" + posterContent;
//
//			/**
//			 * 检查视频文件是否存在
//			 */
//			if(!testFtpFileExists(movieContentUrl)){
//				log.error("movie content not exists:"+movieContentUrl+".");
//				throw new VideoNotFoundException("Video file was not found in FTP directory.");
//			}
//
//			/**
//			 * 检查海报文件是否存在
//			 */
//			if(posterUrl!=null&&!testFtpFileExists(posterUrl)){
//				log.error("poster not exists:"+posterUrl+".");
//				throw new PosterNotFoundException("Poster file was not found in FTP directory.");
//			}
//			
//			/**
//			 * initialize vod content status
//			 */			
//			if (contentStatus == null) {
//				contentStatus = new VodContentStatus();
//				contentStatus.setContId(movieAssetId);
//				contentStatus.setIsValid(1);
//
//				contentStatus.setContentUrl(movieContentUrl);
//				contentStatus.setPosterUrl(posterUrl);
//			} else {
//				if (1 != contentStatus.getAssetStatus()
//						&& 2 != contentStatus.getAssetStatus()) {
//					contentStatus.setContentUrl(movieContentUrl);
//					contentStatus.setPosterUrl(posterUrl);
//				}
//			}
//
//			contentService.saveOrUpdateContentData(contentInfo, contentStatus);
//
////			if(recomm!=null&&"RS2.0".equals(recomm)&&vodRSServiceProxy!=null){
////				try {
////					Vod vod= new Vod();
////					vod.setActor(contentInfo.getActor());
////					vod.setBitrate(contentInfo.getBitrate());
////					vod.setContid(contentInfo.getContid());
////					vod.setCreationDate(contentInfo.getCreationDate());
////					vod.setDirector(contentInfo.getDirector());
////					vod.setExplanation(contentInfo.getExplanation());
////					vod.setGenre(contentInfo.getGenre());
////					vod.setGenre_action(contentInfo.getGenreAction());
////					vod.setGenre_biography(contentInfo.getGenreBiography());
////					vod.setGenre_children(contentInfo.getGenreChildren());
////					vod.setGenre_comedy(contentInfo.getGenreComedy());
////					vod.setGenre_detective(contentInfo.getGenreDetective());
////					vod.setGenre_disaster(contentInfo.getGenreDisaster());
////					vod.setGenre_ethics(contentInfo.getGenreEthics());
////					vod.setGenre_fantastic(contentInfo.getGenreFantastic());
////					vod.setGenre_gangster(contentInfo.getGenreGangster());
////					vod.setGenre_history(contentInfo.getGenreHistory());
////					vod.setGenre_horror(contentInfo.getGenreHorror());
////					vod.setGenre_legendary(contentInfo.getGenreLegendary());
////					vod.setGenre_lovestory(contentInfo.getGenreLovestory());
////					vod.setGenre_martial(contentInfo.getGenreMartial());
////					vod.setGenre_other(contentInfo.getGenreOther());
////					vod.setGenre_political(contentInfo.getGenrePolitical());
////					vod.setGenre_sports(contentInfo.getGenreSports());
////					vod.setGenre_sf(contentInfo.getGenreSf());
////					vod.setGenre_suspense(contentInfo.getGenreSuspense());
////					vod.setHdFlag(contentInfo.getHdFlag());
////					vod.setLanguage(contentInfo.getLanguage());
////					vod.setOrigin(contentInfo.getOrigin());
////					vod.setOriginAmerica(contentInfo.getOriginAmerica());
////					vod.setOriginChina(contentInfo.getOriginChina());
////					vod.setOriginHomgkong(contentInfo.getOriginHongkong());
////					vod.setOriginJapan(contentInfo.getOriginJapan());
////					vod.setOriginOther(contentInfo.getOriginOther());
////					vod.setOriginTaiwan(contentInfo.getOriginTaiwan());
////					vod.setProviderId(contentInfo.getProviderId());
////					vod.setProviderName(contentInfo.getProviderName());
////					vod.setRating(contentInfo.getRating());
////					vod.setReleaseDate(contentInfo.getReleaseDate());
////					vod.setRunTime(contentInfo.getRunTime());
////					vod.setStatus(0);
////					vod.setTitle(contentInfo.getTitle());
////					log.info("start send vod to rs contentID ="+contentInfo.getContid());
////					vodRSServiceProxy.addVodContent(vod);
////					log.info("end send vod to rs contentID ="+contentInfo.getContid());
////				} catch (SeeWProxyException e1) {
////					// TODO Auto-generated catch block
////					log.error("vod contents update for SeeWProxyException",e1);
////				} catch (Exception e2){
////					log.error("vod contents update for other exception ",e2);
////				}
////			  }	
//				
//				
//				log.info("import asset[assetId="+movieAssetId+"] success.");
//			} catch (Exception e) {
//				log.error("ADI import failed:",e);
//				throw e;
//			}
//			
//			
//	}
	
	/**
	 * 解析Title信息
	 * 
	 * @param titleMap
	 * @param dataMap
	 */
	private static void parseTitleMap(Map<String, String> titleMap,Map<String, String> dataMap,VodContentInfo contentInfo) throws Exception{

		if(titleMap==null||dataMap==null){
			log.warn("title asset or title asset dataMap is null.");
			return;
		}
		String product = getValueFromMap("Product", titleMap);
		if (product == null || product.equals(""))
			product = "MOD";
		
		String actor = getValueFromMap(product + "::Actors", dataMap);
		//String studio = getValueFromMap(product + "::Studio", dataMap);
		String director = getValueFromMap(product + "::Director", dataMap);
		String title = getValueFromMap(product + "::Title", dataMap);
		//String category = getValueFromMap(product + "::Category", dataMap);
		
		//String rating = getValueFromMap(product + "::Rating", dataMap);
		String genre = getValueFromMap(product + "::Genre", dataMap);
		/*if(genre!=null&&!"".equals(genre)){
			genre=genre.split(",")[0];
		}
		if(genre==null||"".equals(genre.trim())){
			genre="other";
		}*/
		String licensing_Window_Start = getValueFromMap(product+ "::Licensing_Window_Start", dataMap);
		String runTime=getValueFromMap(product+ "::Run_Time", dataMap);
		//String licensing_Window_End = getValueFromMap(product+ "::Licensing_Window_End", dataMap);
		//String summary_Short = getValueFromMap(product + "::Summary_Short",dataMap);
		//String summary_Medium = getValueFromMap(product + "::Summary_Medium",dataMap);
		String summary_Long = getValueFromMap(product + "::Summary_Long",dataMap);
		String origin=getValueFromMap(product + "::Country_of_Origin",dataMap);

		contentInfo.setTitle(title);
		contentInfo.setExplanation(summary_Long);
		
		if(genre!=null) genre=genre.toLowerCase();
		contentInfo.setGenre(genre);
		
		setContentGenre(contentInfo,genre);
		contentInfo.setDirector(director);
		contentInfo.setReleaseDate(DateTools.formatLicenseWindowDate(licensing_Window_Start));
		contentInfo.setRunTime(runTime);
		contentInfo.setOrigin(origin);
		setContentOrigin(contentInfo,origin);
		contentInfo.setActor(actor);
	}
	
	private static void parseTitleMap2(Map<String, String> titleMap,Map<String, String> dataMap,VodContentInfo contentInfo) throws Exception{
		if(titleMap==null||dataMap==null){
			log.warn("title asset or title asset dataMap is null.");
			return;
		}
		String product = getValueFromMap("Product", titleMap);
		if (product == null || product.equals(""))
			product = "MOD";
		
		String title = getValueFromMap(product + "::Title", dataMap);

		contentInfo.setTitle(title);
	}
	
	
	/**
	 * 解析Movie信息
	 * 
	 * @param titleMap
	 * @param dataMap
	 */
	private static void parseMovieMap(Map<String, String> titleMap,Map<String, String> dataMap,VodContentInfo contentInfo) throws Exception{
		if(titleMap==null||dataMap==null){
			log.warn("[bms-->app]Movie title or Movie dataMap is null.");
			return;
		}
		String product = getValueFromMap("Product", titleMap);
		if (product == null || product.equals(""))
			product = "MOD";
		
		String hDContent = getValueFromMap(product + "::HDContent", dataMap);
		String bit_Rate = getValueFromMap(product + "::Bit_Rate", dataMap);
		
		try{
			contentInfo.setBitrate(Integer.parseInt(bit_Rate));
		}catch(Exception e){
		}
		contentInfo.setHdFlag("Y".equalsIgnoreCase(hDContent)?1:0);
	}
	
	private static String getValueFromMap(String key, Map map) {
		if (map == null)
			return null;
		return map.get(key) == null ? "" : map.get(key).toString();
	}
	
	private static Map<String, Map<String, String>> getMetaData(
			Map<String, Map<String, String>> map, ADIMetadata adidata) {
		if (adidata.getAmsMetadata() != null) {
			Map<String, String> temp = adidata.getAmsMetadata();

			temp.put("Content", adidata.getContent());

			if (temp.get("Asset_Class") != null) {
				map.put(temp.get("Asset_Class").toString(), temp);
			}

			if (adidata.getChildAsset().size() > 0) {
				for (ADIMetadata sub : adidata.getChildAsset()) {
					getMetaData(map, sub);
				}
			}

			if (adidata.getAppMetadata() != null) {
				Map<String, String> appdata = new HashMap<String, String>();
				insertvalue(appdata, adidata.getAppMetadata());
				map.put("APPDATA" + temp.get("Asset_Class"), appdata);
			}
		}

		return map;
	}
	
	private static Map<String, String> insertvalue(Map<String, String> map,
			List<Metadata> list) {
		if (list != null && map != null) {
			for (Metadata meta : list) {
				if (meta.name.indexOf("AMS::") < 0) {
					if (map.get(meta.name) != null) {
						map.put(meta.name, map.get(meta.name)+","+meta.value);
					} else {
						map.put(meta.name, meta.value);
					}
				}

			}
		}
		return map;
	}
	
	private static void setContentGenre(VodContentInfo contentInfo,String genres){
		
		if(genres==null||"".equals(genres.trim())) return;
		
		contentInfo.setGenreOther(0);
		contentInfo.setGenreHorror(0);
		contentInfo.setGenreAction(0);
		contentInfo.setGenreSuspense(0);
		contentInfo.setGenreComedy(0);
		contentInfo.setGenreLovestory(0);
		contentInfo.setGenreHistory(0);
		contentInfo.setGenreSf(0);
		contentInfo.setGenreGangster(0);
		contentInfo.setGenreDisaster(0);
		contentInfo.setGenreEthics(0);
		contentInfo.setGenreBiography(0);
		contentInfo.setGenreSports(0);
		contentInfo.setGenreMartial(0);
		contentInfo.setGenrePolitical(0);
		contentInfo.setGenreDetective(0);
		contentInfo.setGenreLegendary(0);
		contentInfo.setGenreFantastic(0);
		contentInfo.setGenreChildren(0);
		
		boolean isMatchGenre=false;
		
		String[] genreArr=genres.split(",");
		for(String genre:genreArr){
			if("comedy".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreComedy(1);
				isMatchGenre=true;
			}
			else if("sf".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreSf(1);
				isMatchGenre=true;
			}
			else if("gangster".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreGangster(1);
				isMatchGenre=true;
			}
			else if("action".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreAction(1);
				isMatchGenre=true;
			}		
			else if("disaster".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreDisaster(1);
				isMatchGenre=true;
			}		
			else if("ethics".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreEthics(1);
				isMatchGenre=true;
			}			
			else if("biography".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreBiography(1);
				isMatchGenre=true;
			}	
			else if("sports".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreSports(1);
				isMatchGenre=true;
			}	
			else if("lovestory".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreLovestory(1);
				isMatchGenre=true;
			}
			else if("martial".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreMartial(1);
				isMatchGenre=true;
			}
			else if("horror".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreHorror(1);
				isMatchGenre=true;
			}
			else if("history".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreHistory(1);
				isMatchGenre=true;
			}
			else if("political".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenrePolitical(1);
				isMatchGenre=true;
			}
			else if("detective".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreDetective(1);
				isMatchGenre=true;
			}
			else if("suspense".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreSuspense(1);
				isMatchGenre=true;
			}
			else if("legendary".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreLegendary(1);
				isMatchGenre=true;
			}
			else if("fantastic".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreFantastic(1);
				isMatchGenre=true;
			}
			else if("children".equalsIgnoreCase(genre.trim())){
				contentInfo.setGenreChildren(1);
				isMatchGenre=true;
			}else{
				
			}
		}
		
		if(!isMatchGenre){
			contentInfo.setGenreOther(1);
		}
	}
	
	private static void setContentOrigin(VodContentInfo content,String origin){
		if(origin==null) return;
		content.setOriginChina(0);
		content.setOriginAmerica(0);
		content.setOriginJapan(0);
		content.setOriginKorea(0);
		content.setOriginTaiwan(0);
		content.setOriginHongkong(0);
		content.setOriginOther(0);
		
		if("china".equalsIgnoreCase(origin.trim())||"cn".equalsIgnoreCase(origin.trim())){
			content.setOriginChina(1);
		}else if("america".equalsIgnoreCase(origin.trim())||"us".equalsIgnoreCase(origin.trim())||"usa".equalsIgnoreCase(origin.trim())){
			content.setOriginAmerica(1);
		}else if("japan".equalsIgnoreCase(origin.trim())||"jp".equals(origin.trim())){
			content.setOriginJapan(1);
		}else if("korea".equalsIgnoreCase(origin.trim())){
			content.setOriginKorea(1);
		}else if("taiwan".equalsIgnoreCase(origin.trim())||"tw".equals(origin.trim())){
			content.setOriginTaiwan(1);
		}else if("hongkong".equalsIgnoreCase(origin.trim())||"hk".equals(origin.trim())){
			content.setOriginHongkong(1);
		}else{
			content.setOriginOther(1);
		}
	}
	
	private static boolean testFtpFileExists(String ftpUrl) throws Exception {
		log.info("testFtpFileExists(String ftpUrl):"+ftpUrl);
		FtpClient ftpClient = new FtpClient();
		ftpClient.setTimeout(300000);
		
		
		try {
			
			URL url = null;
			try {
				url = new URL(ftpUrl);
			} catch (MalformedURLException exception) {
				log.error("new URL",exception);
				return false;
			}
			log.info("url ok!!!");
			HostInfoExtractor hostInfoExtractor = new HostInfoExtractor(url);

			hostInfoExtractor.decorateNetworkProtocolClient(ftpClient);
			
			log.info("hostInfoExtractor.decorateNetworkProtocolClient ok!!!");
			try {
				ftpClient.doLogin();
			} catch (Exception e) {
				log.error("ftp login failed",e);
			}
			log.info("ftp login ok!!!");
			if (ftpClient.isLogined() == false) {
				return false;
			}
			
			if (ftpClient.cdToRemoteDir(hostInfoExtractor.extractTheFolder()) == false) {
				return false;
			}
			log.info("ftpClient.cdToRemoteDir(hostInfoExtractor.extractTheFolder() ok!!!");
			ftpClient.testRemoteFileExists(hostInfoExtractor.extractFileName());
			log.info("ftpClient.testRemoteFileExists(hostInfoExtractor.extractFileName() ok!!!");

		} catch (ConnectionException ce) {
			return false;

		}  catch(RemoteFtpFileNotExistsException rfe){
			log.error("Connection ftp error :"+rfe.getMessage());
			return false;
		}
		finally {
			try {
				ftpClient.close();
			} catch (Exception e) {
				log.error("Connection ftp error :"+e.getMessage());
			}
		}
		return true;
	}	
	
	public static void main(String[] args){
		String url1="ftp://vision:vision@192.168.28.120:21/ftp/persistADI/Vod/titl2011201100250202/Z7101007080002.png";
		String url2="ftp://vision:vision@192.168.28.120:21/ftp/persistADI/Vod/titl2011201100250204/Z7101006180025.png";
		String url3="ftp://vision:vision@192.168.28.120:21/ftp/persistADI/Vod/titl2011201100250205/Z7101007080001.png";
		String url4="ftp://vision:vision@192.168.28.120:21/ftp/persistADI/Vod/titl2011201100250206/Z7101006180024.png";
		String url5="ftp://vision:vision@192.168.28.120:21/ftp/persistADI/Vod/titl2011201100250207/Z7101006180034.png";
		String url6="ftp://vision:vision@192.168.28.120:21/ftp/persistADI/Vod/titl2011201100250209/Z7101006180016.png";
		String url7="ftp://vision:vision@192.168.28.120:21/ftp/persistADI/Vod/titl2011201100250208/Z7101006180012.png";
		String url8="ftp://vision:vision@192.168.28.120:21/ftp/persistADI/Vod/titl2011201100250210/Z7101006180019.png";
		String url9="ftp://vision:vision@192.168.28.120:21/ftp/persistADI/Vod/titl2011201100250203/Z7101006180009.png";
		String url10="ftp://vision:vision@192.168.28.120:21/ftp/persistADI/Vod/titl2011201100250201/Z7101006180039.png";
		List<String> list=new java.util.ArrayList<String>();
		list.add(url1);
		list.add(url2);
		list.add(url3);
		list.add(url4);
		list.add(url5);
		list.add(url6);
		list.add(url7);
		list.add(url8);
		list.add(url9);
		list.add(url10);
		try {
			for (int j = 0; j < 20; j++) {
				for (int i = 0; i < list.size(); i++) {
					String url = list.get(i);
					System.out.println(url);
					System.out.println(testFtpFileExists(url));
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
}
